﻿namespace IBTrader.Indicator.CandleStick
{
    using IBTrader.Modules.Read;
    using IBTrader.Charts;
    using System;

    class CandleStickPattern : Base // CandleStick
    {
        public CandleStickPattern(Worker worker, int seconds = 60) : base(worker, seconds) { }
        protected override void Add(object _, Item item)
        {
            //base.Add(sender, item);
        }
        protected override Item NewItem()
        {
            return new PatternItem(){ OnHit = OnHit };
        }
        public class PatternItem : Item
        {
            private static readonly Item[] Items = new Item[2];
            internal Action<Line> OnHit;
            protected internal override void Add(Line line)
            {
                if (OpenLine==null)
                {
                    if (Items[0] != null)
                    {
                        if (Items[0].Open < Items[0].Close && Items[0].Close < Items[1].Open && Items[1].Open < Items[1].Close && Items[1].Close < line.Price)
                        {
                            OnHit(Items[1]);
                        }
                        if (Items[0].Open > Items[0].Close && Items[0].Close > Items[1].Open && Items[1].Open > Items[1].Close && Items[1].Close > line.Price)
                        {
                            OnHit(Items[1]);
                        }                        
                    }
                    Items[0] = Items[1];
                    Items[1] = this;
                }
                base.Add(line);
            }
        }
    }
}